package net.office.service.impl;

import net.office.bean.WorkTable;
import net.office.bean.WorkTableExample;
import net.office.mapper.WorkTableMapper;
import net.office.service.WorkTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class WorkTableServiceImpl implements WorkTableService {

    @Autowired
    private WorkTableMapper workTableMapper;

    @Override
    public List<WorkTable> findMonthByStaffId(Integer staffId, Date date) {
        Calendar begin_calendar = Calendar.getInstance();
        begin_calendar.setTime(date);
        begin_calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date begin_date = begin_calendar.getTime();

        Calendar end_calendar = Calendar.getInstance();
        end_calendar.setTime(date);
        end_calendar.add(Calendar.MONTH, 1);
        end_calendar.set(Calendar.DAY_OF_MONTH, 0);
        Date end_date = end_calendar.getTime();

        WorkTableExample example = new WorkTableExample();
        example.createCriteria().andWorkStaffIdEqualTo(staffId).andWorkDateBetween(begin_date, end_date);

        return workTableMapper.selectByExample(example);
    }

    @Override
    public List<WorkTable> findMonthByDate(Date date) {
        Calendar begin_calendar = Calendar.getInstance();
        begin_calendar.setTime(date);
        begin_calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date begin_date = begin_calendar.getTime();

        Calendar end_calendar = Calendar.getInstance();
        end_calendar.add(Calendar.MONTH, 1);
        end_calendar.set(Calendar.DAY_OF_MONTH, 0);
        Date end_date = end_calendar.getTime();

        WorkTableExample example = new WorkTableExample();
        example.createCriteria().andWorkDateBetween(begin_date, end_date);

        return workTableMapper.selectByExample(example);
    }

    @Override
    public List<WorkTable> findOneByDate(Integer staffId, Date date) {
        /*Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date date = calendar.getTime();*/

        WorkTableExample example = new WorkTableExample();
        example.createCriteria().andWorkDateEqualTo(date);

        List<WorkTable> workTableList = workTableMapper.selectByExample(example);

        return workTableMapper.selectByExample(example);
    }

    @Override
    public int insert(WorkTable workTable) {
        return workTableMapper.insert(workTable);
    }

    @Override
    public int updateByPrimaryKey(List<WorkTable> workTableList) {
        int res = 0;
        for(int i=0; i<workTableList.size(); i++){
            res = res + workTableMapper.updateByPrimaryKey(workTableList.get(i));
        }

        return res;
    }
}
